package summer.codegenerator.model.base;

import java.lang.reflect.Type;

import summer.codegenerator.common.util.ClazzUtil;

import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONLexer;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;

/**
 * 
 * 
 * @author 蒋振
 * @since 2019年6月26日 下午2:51:40
 */
public class EnumFastJSONDeserializer implements ObjectDeserializer {

	@SuppressWarnings("unchecked")
	@Override
	public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
		final JSONLexer lexer = parser.lexer;
		Class<?> cls = (Class<?>) type;
		try {
			Object enumObj = ClazzUtil.getEnumObjByClassObjAndEnumIndex(cls, lexer.intValue());
			return (T) enumObj;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int getFastMatchToken() {
		return 0;
	}
}
